package com.young.coordin.server.web;

import com.young.common.util.ClientInfoUtil;
import com.young.common.util.JsonUtil;
import com.young.common.util.Result;
import com.young.common.util.StringUtils;
import com.young.coordin.server.model.CoordinResultEnum;
import com.young.coordin.server.service.ICoordinRegisterService;
import com.young.coordin.server.util.CoordinResponseBuilder;
import com.young.interfaces.coordin.model.HeartbeatDTO;
import com.young.interfaces.coordin.model.RegisterDTO;
import com.young.interfaces.coordin.model.CoordinResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 协调中心网关
 * Created by Admin on 2020/12/10.
 */
@Controller
@RequestMapping(value = "/coordin/server", method = {RequestMethod.POST})
public class CoordinRegisterController {

    private static final Logger logger = LoggerFactory.getLogger(CoordinRegisterController.class);
    /**
     * 协调服务
     */
    @Autowired
    ICoordinRegisterService coordinRegisterService;

    /**
     * 注册
     * @param request
     * @param registerDTO
     * @return
     */
    @RequestMapping("/register")
    @ResponseBody
    public CoordinResponse register(HttpServletRequest request, RegisterDTO registerDTO) {
        logger.info("[协调中心-注册] 入参={}", JsonUtil.toJson(registerDTO));
        //补全ip,端口号
        if (StringUtils.isBlank(registerDTO.getIp())) {
            registerDTO.setIp(ClientInfoUtil.getClientIpAddr(request));
        }
        if (registerDTO.getPort() == null) {
            registerDTO.setPort(request.getRemotePort());
        }
        if (registerDTO.getTimestamp() == null || CollectionUtils.isEmpty(registerDTO.getTopics())
                || StringUtils.isBlank(registerDTO.getIp()) || registerDTO.getPort() == null) {
            logger.warn("[协调中心-心跳] 存在参数为空,入参={}", JsonUtil.toJson(registerDTO));
            return CoordinResponseBuilder.buildFail(CoordinResultEnum.PARAM_ERROR);
        }
        return coordinRegisterService.register(registerDTO);
    }

    /**
     * 心跳服务
     * @param request
     * @param heartbeatDTO
     * @return
     */
    @RequestMapping("/heartbeat")
    @ResponseBody
    public CoordinResponse heartbeat(HttpServletRequest request, HeartbeatDTO heartbeatDTO) {
        logger.info("[协调中心-心跳] 入参={}", JsonUtil.toJson(heartbeatDTO));
        //补全ip,端口号
        if (StringUtils.isBlank(heartbeatDTO.getIp())) {
            heartbeatDTO.setIp(ClientInfoUtil.getClientIpAddr(request));
        }
        if (heartbeatDTO.getPort() == null) {
            heartbeatDTO.setPort(request.getRemotePort());
        }
        if (heartbeatDTO.getTimestamp() == null || heartbeatDTO.getVersion() == null
                || StringUtils.isBlank(heartbeatDTO.getIp()) || heartbeatDTO.getPort() == null) {
            logger.warn("[协调中心-心跳] 存在参数为空,入参={}", JsonUtil.toJson(heartbeatDTO));
            return CoordinResponseBuilder.buildFail(CoordinResultEnum.PARAM_ERROR);
        }
        //心跳服务
        return coordinRegisterService.heartbeat(heartbeatDTO);
    }


}
